setting.callback.onClick = getEquipData;

$(document).ready(function(){

	$('#datagrid-toolbar #search_link').linkbutton({
	    onClick:function(){
	    	search(page_);
	    }
	});
	
	/**
	 * 绑定重置事件
	 */
	$('#datagrid-toolbar #reset_link').linkbutton({
		onClick:function(){
			 $("#beginDate").datebox("setValue","");
			 $("#endDate").datebox("setValue","");
		}
	});
});

/**
 * 获取部门数据
 * @param data
 */
function getEquipData(event,treeId,treeNode){
	var id = (treeNode.id);
	if(id.indexOf("station_")!=-1){
		search(page_);
	}else{
		message("请选择站点查询数据");
	}
}

/**
 * 定义分页全局变量
 */
var page_ = new page({search:search,callback:func});

/**
 * 定义容器全局变量
 */
var con = new container({
	datagrid:'#datagrid',
	datagridtoolbar:'#datagrid-toolbar', 
	singleSelect:true});

/**
 * 查询
 * @param page
 */
function search(pg){
	if(pg == undefined || pg == null){
		pg = page_;
	}
	var url = ctx + "/equipData/query?comeFrom=search";
	page_.pageSize = pg.pageSize;
	page_.pageNumber = pg.pageNumber;
	query(url, getCondition(), con, page_);
}

/**
 * 获取查询条件
 * @returns {condition}
 */
function getCondition(){
	
	var organTree = $.fn.zTree.getZTreeObj("organTree");
	var organ = organTree.getSelectedNodes();
	
	var condition = {};
	
	condition.stationId = organ[0].id;

	condition.beginDate = $('#listform #beginDate').datebox('getValue');
	condition.endDate = $('#listform #endDate').datebox('getValue');
	if($.trim(condition.beginDate).length != 0){
		condition.beginDate = condition.beginDate + " 00:00:00";
	}
	if($.trim(condition.endDate).length != 0){
		condition.endDate = condition.endDate + " 23:59:59";
	}
	
	return condition;
}

/**
 * 回调函数
 */
function func(){
	$("#datagrid").datagrid({onClickRow:function(index,row){
	}});
}


/**
 * 详情
 */
function detail(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
		title: '详情',
		width: 800,
		height: 600,
		closed: false,
		cache: false,
		modal: true,
		href:ctx + "/web/data/equip_data/detail.jsp",
		onClose:function(){
			destroy('dialog','#dialogDiv');
		},
		onLoad:function(){
			$("#addform").form('load',{});
			$(".result-text").html("");
			$("#stationName").html(row.stationName);
			$("#equipName").html(row.equipName);
			$("#systemName").html(row.systemName);
			$("#collectionTime").html(row.collectionTime);

			$("#plateNo").html(row.plateNo);
			$("#plateColor").html(row.plateColor);
			$("#trainType").html(row.trainType);
			
			$("#axleNumber").html(row.axleNumber);
			$("#wheelNumber").html(row.wheelNumber);
			
			$("#wheelNumber1").html(row.wheelNumber1);
			$("#wheelNumber2").html(row.wheelNumber2);
			$("#wheelNumber3").html(row.wheelNumber3);
			$("#wheelNumber4").html(row.wheelNumber4);
			$("#wheelNumber5").html(row.wheelNumber5);
			$("#wheelNumber6").html(row.wheelNumber6);
			$("#wheelNumber7").html(row.wheelNumber7);
			$("#wheelNumber8").html(row.wheelNumber8);
			$("#wheelNumber9").html(row.wheelNumber9);
			$("#wheelNumber10").html(row.wheelNumber10);
			
			$("#totalWeight").html(row.totalWeight);
			$("#wheelWeight").html(row.wheelWeight);
			
			$("#axleWeight1").html(row.axleWeight1);
			$("#axleWeight2").html(row.axleWeight2);
			$("#axleWeight3").html(row.axleWeight3);
			$("#axleWeight4").html(row.axleWeight4);
			$("#axleWeight5").html(row.axleWeight5);
			$("#axleWeight6").html(row.axleWeight6);
			$("#axleWeight7").html(row.axleWeight7);
			$("#axleWeight8").html(row.axleWeight8);
			$("#axleWeight9").html(row.axleWeight9);
			$("#axleWeight10").html(row.axleWeight10);
			
			$("#axleBase1").html(row.axleBase1);
			$("#axleBase2").html(row.axleBase2);
			$("#axleBase3").html(row.axleBase3);
			$("#axleBase4").html(row.axleBase4);
			$("#axleBase5").html(row.axleBase5);
			$("#axleBase6").html(row.axleBase6);
			$("#axleBase7").html(row.axleBase7);
			$("#axleBase8").html(row.axleBase8);
			$("#axleBase9").html(row.axleBase9);
			$("#axleBase10").html(row.axleBase10);
			
			$("#overrunWeight").html(row.overrunWeight);
			$("#goodsType").html(row.goodsType);
			$("#speed").html(row.speed);
			$("#tagName").html(row.tagName);
			$("#status").html(row.cnStatus);
			$("#labelName").html(row.labelName);
		},
		buttons: []
	});
	if(row.tag==0){
		$("#dataTableDiv").find(".datagrid-row-selected").find("[field$=tagName]").find("div").html("已读");
		var josn={
			id:row.id
		}
		$.ajax({
			type : "get",
			url : ctx + "/equipData/read",
			data :josn,
			dataType : "json",
			success : function(data) {
			}
		});
	}
}

/**
 * 修改
 */
function addMark(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
		title: '标记',
		width: 600,
		height: 400,
		closed: false,
		cache: false,
		modal: true,
		href:ctx + "/web/data/equip_data/mark.jsp",
		onClose:function(){
			destroy('dialog','#dialogDiv');
		},
		onLoad:function(){
			var dataId = row.id;
			$("#addform #id").val(dataId);

			var markUrl=ctx + "/equipData/getMarkData.json?dataId="+dataId;
			$('#markLabels').combobox('reload', markUrl);
		},
		buttons: [{
			text:'保存',
			iconCls:'icon-ok',
			handler:function(){
				var bool = $("#addform").form('enableValidation').form('validate');
				if(!bool){
					return;
				}
				var data = $("#addform").serializeJson();

				$.ajax({
					type : "get",
					url : ctx + "/equipData/updateMark",
					data :data,
					dataType : "json",
					success : function(data) {
						if(!showMsg(data,"标记成功")){
							return;
						}

						search(page_);

						$('#dialog').dialog('close');
					}
				});
			}
		},{
			text:'取消',
			iconCls:'icon-cancel',
			handler:function(){
				$('#dialog').dialog('close');
			}
		}]
	});
}

function showFile(){
	var row = null;
	row = getSelectedRow("#datagrid");
	if(!row){
		return;
	}
	$('#dialog').dialog({
		title: '附件',
		width: 800,
		height: 600,
		closed: false,
		cache: false,
		modal: true,
		href:ctx + "/web/data/equip_data/file.jsp",
		onClose:function(){
			destroy('dialog','#dialogDiv');
		},
		onLoad:function(){
			$("#file_dataId").val(row.id);
		},
		buttons: [{
			text:'取消',
			iconCls:'icon-cancel',
			handler:function(){
				$('#dialog').dialog('close');
			}
		}]
	});
}